package cn.com.surker.pages.console;

import cn.com.surker.base.system.entity.SysControlStorage;
import cn.com.surker.base.system.entity.SysStorage;
import cn.com.surker.base.system.service.SysStorageService;
import cn.com.surker.common.AbstractController;
import cn.com.surker.enums.ICodeDescEnum;
import cn.com.surker.enums.material.*;
import cn.com.surker.enums.system.BillEnum;
import cn.com.surker.enums.system.DisplayEnum;
import cn.com.surker.enums.system.SupplierTypeEnum;
import cn.com.surker.enums.weight.OrderWeightTypeEnum;
import cn.com.surker.enums.weight.WeightCarTypeEnum;
import cn.com.surker.material.entity.MainMaterial;
import cn.com.surker.material.service.MainMaterialContractService;
import cn.com.surker.material.service.MainMaterialService;
import cn.com.surker.material.service.MainSupplierService;
import cn.com.surker.oa.entity.OaMaterial;
import cn.com.surker.oa.entity.OaSupplier;
import cn.com.surker.redis.RedisUtils;
import cn.com.surker.system.entity.SysMenuRoleController;
import cn.com.surker.system.entity.SysOrigin;
import cn.com.surker.system.entity.SysUser;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.system.service.SysControlStorageService;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lucky
 * @create 2024年07月05 7:51
 * @desc
 */
@Slf4j
@Controller
@RequestMapping("/console/main-material")
public class MaterialIndexController extends AbstractController {

    @Resource
    private MainMaterialService materialService;
    @Resource
    private MainMaterialContractService mainMaterialContractService;
    @Resource
    private SysStorageService sysStorageService;
    @Resource
    private SysControlStorageService sysControlStorageService;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private MainSupplierService supplierService;

    /**
     * 材料合同
     * @return
     */
    @RequestMapping("/contract")
    public ModelAndView materialContract() {
        ModelAndView modelAndView = new ModelAndView();

        SysUser user = getUser();
        if (user == null) {//未登录用户
            modelAndView.setViewName("console/system/login");
            return modelAndView;
        }
        List<Map<String, Object>> qualityList = new ArrayList<>();
        List<Map<String, Object>> typeList = new ArrayList<>();
//        List<MainSupplier> supplierList = mainMaterialContractService.findSupplier();
        List<SysOrigin> orgList = getUserOrgList();
        List<OaMaterial> materialList = materialService.findAll();
        List<Map<String, Object>> billList = new ArrayList<>();
        for (int i = 0; i < MaterialQualityEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", MaterialQualityEnum.values()[i].getCode());
            map.put("name", MaterialQualityEnum.values()[i].getDescription());
            qualityList.add(map);
        }
        for (int i = 0; i < MaterialContractTypeEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", MaterialContractTypeEnum.values()[i].getCode());
            map.put("name", MaterialContractTypeEnum.values()[i].getDescription());
            typeList.add(map);
        }
        for (int i = 0; i < BillEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", BillEnum.values()[i].getCode());
            map.put("name", BillEnum.values()[i].getDescription());
            billList.add(map);
        }

        SysMenuRoleController menuRole = isControlVerify("/console/main-material/contract");
        modelAndView.addObject("menuRole", menuRole);//权限验证
        modelAndView.addObject("billList", billList);
        modelAndView.addObject("typeList", typeList);
        modelAndView.addObject("qualityList", qualityList);
        modelAndView.addObject("orgList", orgList);
//        modelAndView.addObject("supplierList", supplierList);
        modelAndView.addObject("materialList", materialList);
        modelAndView.setViewName("console/material/contract");
        return modelAndView;
    }
    /**
     * 材料管理
     * @return
     */
    @RequestMapping("/material")
    public ModelAndView material() {
        ModelAndView modelAndView = new ModelAndView();

        SysUser user = getUser();
        if (user == null) {//未登录用户
            modelAndView.setViewName("console/system/login");
            return modelAndView;
        }
        List<Map<String, Object>> breedTypeList = new ArrayList<>();
        List<Map<String, Object>> weightBillList = new ArrayList<>();
        List<Map<String, Object>> storeList = new ArrayList<>();
        List<Map<String, Object>> displayList = new ArrayList<>();
        List<Map<String, Object>> materialTypeList = new ArrayList<>();
        List<OaMaterial> materialList = materialService.findAll();
        List<SysOrigin> orgList = getUserOrgList();
        for (int i = 0; i < SysMaterialTypeEnum.values().length;i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", SysMaterialTypeEnum.values()[i].getCode());
            map.put("name", SysMaterialTypeEnum.values()[i].getDescription());
            materialTypeList.add(map);
        }
        for (int i = 0; i < DisplayEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", DisplayEnum.values()[i].getCode());
            map.put("name", DisplayEnum.values()[i].getDescription());
            displayList.add(map);
        }
        for (int i = 0; i < MaterialStoreTypeEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", MaterialStoreTypeEnum.values()[i].getCode());
            map.put("name", MaterialStoreTypeEnum.values()[i].getDescription());
            storeList.add(map);
        }
        for (int i = 0; i < MaterialBreedTypeEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", MaterialBreedTypeEnum.values()[i].getCode());
            map.put("name", MaterialBreedTypeEnum.values()[i].getDescription());
            breedTypeList.add(map);
        }
        for (int i = 0; i < BillWeightTypeEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", BillWeightTypeEnum.values()[i].getCode());
            map.put("name", BillWeightTypeEnum.values()[i].getDescription());
            weightBillList.add(map);
        }
        SysMenuRoleController menuRole = isControlVerify("/console/main-material/material");
        modelAndView.addObject("menuRole", menuRole);//权限验证
        modelAndView.addObject("weightBillList", weightBillList);
        modelAndView.addObject("breedTypeList", breedTypeList);
        modelAndView.addObject("storeList", storeList);
        modelAndView.addObject("displayList", displayList);
        modelAndView.addObject("orgList", orgList);
        modelAndView.addObject("materialTypeList", materialTypeList);
        modelAndView.addObject("materialList", materialList);
        modelAndView.setViewName("console/material/material");
        return modelAndView;
    }

    /**
     * 材料供应商页面跳转
     * @return
     */
    @RequestMapping("/mainsupplier")
    public ModelAndView mainsupplier() {
        ModelAndView modelAndView = new ModelAndView();
        SysUser user = getUser();
        if (ObjectUtil.isEmpty(user)) {//未登录用户
            modelAndView.setViewName("console/system/login");
            return modelAndView;
        }
        List<Map<String, Object>> supplierTypeList = new ArrayList<>();
        for (int i = 0; i < SupplierTypeEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", SupplierTypeEnum.values()[i].getCode());
            map.put("name", SupplierTypeEnum.values()[i].getDescription());
            supplierTypeList.add(map);
        }
        List<SysOrigin> orgList = getUserOrgList();
        SysMenuRoleController menuRole = isControlVerify("/console/main-material/mainsupplier");
        modelAndView.addObject("menuRole", menuRole);//权限验证
        modelAndView.addObject("orgList", orgList);
        modelAndView.addObject("supplierTypeList", supplierTypeList);
        modelAndView.setViewName("console/material/mainsupplier");
        return modelAndView;
    }


    /**
     * 材料扣重页面
     * @return
     */
    @RequestMapping("/material-discount")
    public ModelAndView mainmaterialdiscount() {
        ModelAndView modelAndView = new ModelAndView();
        SysUser user = getUser();
        if (ObjectUtil.isEmpty(user)) {//未登录用户
            modelAndView.setViewName("console/system/login");
            return modelAndView;
        }
        List<Map<String, Object>> materialBreedList = new ArrayList<>();
        for (int i = 0; i < MaterialBreedTypeEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", MaterialBreedTypeEnum.values()[i].getCode());
            map.put("name", MaterialBreedTypeEnum.values()[i].getDescription());
            materialBreedList.add(map);
        }
        List<SysOrigin> orgList = getUserOrgList();
        SysMenuRoleController menuRole = isControlVerify("/console/main-material/material-discount");
        modelAndView.addObject("menuRole", menuRole);//权限验证
        modelAndView.addObject("orgList", orgList);
        modelAndView.addObject("materialBreedList", materialBreedList);
        modelAndView.setViewName("console/material/material-discount");
        return modelAndView;
    }

    /**
     * 材料抽检概率页面跳转
     * @return
     */
    @RequestMapping("/supplier-rate")
    public ModelAndView materialSupplierRate() {
        ModelAndView modelAndView = new ModelAndView();
        SysUser user = getUser();
        if (ObjectUtil.isEmpty(user)) {//未登录用户
            modelAndView.setViewName("console/system/login");
            return modelAndView;
        }
        SysMenuRoleController menuRole = isControlVerify("/console/main-material/supplier-rate");
        modelAndView.addObject("menuRole", menuRole);//权限验证
        List<SysOrigin> orgList = getUserOrgList();
        modelAndView.addObject("orgList", orgList);
        modelAndView.setViewName("console/material/material-supplier-rate");
        return modelAndView;
    }


    /**
     * 材料限量 页面跳转
     * @return
     */
    @RequestMapping("/material-limit")
    public ModelAndView materialLimit() {
        ModelAndView modelAndView = new ModelAndView();
        SysMenuRoleController menuRole = isControlVerify("/console/main-material/material-limit");
        modelAndView.addObject("menuRole", menuRole);//权限验证
        List<SysOrigin> orgList = getUserOrgList();
        modelAndView.addObject("orgList", orgList);
        modelAndView.setViewName("console/material/material-limit");
        return modelAndView;
    }

    /**
     * 原材过磅
     * @return
     */
    @RequestMapping("/material-weight")
    public ModelAndView weight() {
        ModelAndView modelAndView = new ModelAndView();
        CacheUser user = getUser();
        List<SysOrigin> orgList = getUserOrgList();
        List<SysStorage> storageList = new ArrayList<>();
        List<OaSupplier> supplierList = new ArrayList<>();
        List<OaMaterial> materialList = new ArrayList<>();
        if (orgList != null && orgList.size() > 0) {
            storageList = sysStorageService.findByOrg(orgList.get(0).getId(), null, null);
            supplierList = supplierService.findSupplierByOrg(orgList.get(0).getId());
            materialList = materialService.findByOrg(orgList.get(0).getId());
        }
        ///console/main-material/material-weight

        List<Map<String, Object>> weightTypeList = new ArrayList<>();
//        List<OaMaterial> materialList = materialService.findAll();
        for (int i = 0; i < OrderWeightTypeEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", OrderWeightTypeEnum.values()[i].getCode());
            map.put("name", OrderWeightTypeEnum.values()[i].getDescription());
            weightTypeList.add(map);
        }
        Integer isAdmin = 0;//是否管理员
        Integer isCheck = 0;//是否材料统计
        Integer isFinance = 0;//是否财务
        if (user.getRoleNameList().contains("系统管理员") || user.getRoleNameList().contains("软件工程师")
                || user.getRoleNameList().contains("网络工程师")) {
            isAdmin = 1;
        }
        if (user.getRoleNameList().contains("材料统计") || user.getRoleNameList().contains("材料与采购经理")) {
            isCheck = 1;
        }
        if (user.getRoleNameList().contains("财务统计") || user.getRoleNameList().contains("财务经理")) {
            isFinance = 1;
        }
        SysMenuRoleController menuRole = isControlVerify("/console/main-material/material-weight");
        modelAndView.addObject("menuRole", menuRole);//权限验证
        modelAndView.addObject("isAdmin", isAdmin);
        modelAndView.addObject("isCheck", isCheck);
        modelAndView.addObject("isFinance", isFinance);
        modelAndView.addObject("materialList", materialList);
        modelAndView.addObject("storageList", storageList);
        modelAndView.addObject("supplierList", supplierList);
        modelAndView.addObject("weightTypeList", weightTypeList);
        modelAndView.addObject("orgList", orgList);
        modelAndView.setViewName("console/material/material-weight");
        return modelAndView;
    }
    /**
     * 实时库存 页面跳转
     * @return
     */
    @RequestMapping("/real-time-store")
    public ModelAndView realTimeStore() {
        ModelAndView modelAndView = new ModelAndView();
//        SysUser user = getUser();
//        if (ObjectUtil.isEmpty(user)) {//未登录用户
//            modelAndView.setViewName("console/system/login");
//            return modelAndView;
//        }
        List<SysOrigin> orgList = getUserOrgList();
        modelAndView.addObject("orgList", orgList);
        modelAndView.setViewName("console/material/real-time-store");
        return modelAndView;
    }
    /**
     * 材料限量 页面跳转
     * @return
     */
    @RequestMapping("/material-car-weight")
    public ModelAndView materialWeight() {
        ModelAndView modelAndView = new ModelAndView();
        SysUser user = getUser();
//        if (ObjectUtil.isEmpty(user)) {//未登录用户
//            modelAndView.setViewName("console/system/login");
//            return modelAndView;
//        }
        List<SysOrigin> orgList = getUserOrgList();
        SysMenuRoleController menuRole = isControlVerify("/console/main-material/material-car-weight");
        List<Map<String, Object>> weightList = ICodeDescEnum.getEnumDatasV1(OrderWeightTypeEnum.class);
        List<Map<String, Object>> carTypeList = ICodeDescEnum.getEnumDatasV1(WeightCarTypeEnum.class);
        modelAndView.addObject("menuRole", menuRole);//权限验证
        modelAndView.addObject("orgList", orgList);
        modelAndView.addObject("weightList", weightList);
        modelAndView.addObject("carTypeList", carTypeList);
        modelAndView.setViewName("console/material/material-car-weight");
        return modelAndView;
    }
    @RequestMapping("/stock-v1")
    public ModelAndView stockV1() {
        ModelAndView modelAndView = new ModelAndView();
        SysUser user = getUser();
//        if (ObjectUtil.isEmpty(user)) {//未登录用户
//            modelAndView.setViewName("console/system/login");
//            return modelAndView;
//        }
        List<Map<String, Object>> inventoryTypeList = new ArrayList<>();//历史盘存类型
        for (int i = 0; i < MaterialInventoryEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", MaterialInventoryEnum.values()[i].getCode());
            map.put("name", MaterialInventoryEnum.values()[i].getDescription());
            inventoryTypeList.add(map);
        }
        List<SysOrigin> orgList = getUserOrgList();
        SysMenuRoleController menuRole = isControlVerify("/console/main-material/stock");
        modelAndView.addObject("menuRole", menuRole);//权限验证
        modelAndView.addObject("inventoryTypeList", inventoryTypeList);
        modelAndView.addObject("orgList", orgList);
        modelAndView.setViewName("console/board/stock-v1");
        return modelAndView;
    }
    @RequestMapping("/stock")
    public ModelAndView stock() {
        ModelAndView modelAndView = new ModelAndView();
        SysUser user = getUser();
//        if (ObjectUtil.isEmpty(user)) {//未登录用户
//            modelAndView.setViewName("console/system/login");
//            return modelAndView;
//        }
        List<Map<String, Object>> inventoryTypeList = new ArrayList<>();//历史盘存类型
        for (int i = 0; i < MaterialInventoryEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", MaterialInventoryEnum.values()[i].getCode());
            map.put("name", MaterialInventoryEnum.values()[i].getDescription());
            inventoryTypeList.add(map);
        }
        List<SysOrigin> orgList = getUserOrgList();
        SysMenuRoleController menuRole = isControlVerify("/console/main-material/stock");
        modelAndView.addObject("menuRole", menuRole);//权限验证
        modelAndView.addObject("inventoryTypeList", inventoryTypeList);
        modelAndView.addObject("orgList", orgList);
        modelAndView.setViewName("console/material/stock");
        return modelAndView;
    }
    @RequestMapping("/history-inventory")
    public ModelAndView historyInventory() {
        ModelAndView modelAndView = new ModelAndView();
        List<Map<String, Object>> inventoryTypeList = new ArrayList<>();//历史盘存类型
        for (int i = 0; i < MaterialInventoryEnum.values().length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", MaterialInventoryEnum.values()[i].getCode());
            map.put("name", MaterialInventoryEnum.values()[i].getDescription());
            inventoryTypeList.add(map);
        }
        List<SysOrigin> orgList = getUserOrgList();
        List<SysStorage> storageList = sysStorageService.findByOrg(orgList.get(0).getId(), null, null);
        List<MainMaterial> materialList = materialService.getListMaterialByOrgId(orgList.get(0).getId());
        List<SysControlStorage> controlStorageList = sysControlStorageService.findAll();
        SysMenuRoleController menuRole = isControlVerify("/console/main-material/history-inventory");
        modelAndView.addObject("menuRole", menuRole);//权限验证
        modelAndView.addObject("inventoryTypeList", inventoryTypeList);
        modelAndView.addObject("controlStorageList", controlStorageList);
        modelAndView.addObject("storageList", storageList);
        modelAndView.addObject("materialList", materialList);
        modelAndView.addObject("orgList", orgList);
        modelAndView.setViewName("console/material/history-inventory");
        return modelAndView;
    }


    @RequestMapping("/material-bill")
    public ModelAndView materialBill() {
        ModelAndView modelAndView = new ModelAndView();
        CacheUser user = getUser();
//        if (ObjectUtil.isEmpty(user)) {//未登录用户
//            modelAndView.setViewName("console/system/login");
//            return modelAndView;
//        }
        int isFinance = 0;
        int isCheck = 0;
        if(user.getRoleNameList().contains("统计") || user.getRoleNameList().contains("财务经理")){
            isFinance = 1;
        }
        if(user.getRoleNameList().contains("材料统计") || user.getRoleNameList().contains("材料与采购经理")){
            isCheck = 1;
        }
        List<SysOrigin> orgList = getUserOrgList();
        modelAndView.addObject("isFinanceVal", isFinance);
        modelAndView.addObject("isCheckVal", isCheck);
        modelAndView.addObject("orgList", orgList);
        modelAndView.addObject("orgList", orgList);
        modelAndView.setViewName("console/material/material-bill");
        return modelAndView;
    }


}
